'''
Case Type： 故障&可靠性测试
Case Name： 数据库备机控制文件权限丢失，主动恢复权限后，数据库备机重启。
Create At: 2020/07/20
@zou_jialiang0505328114
Descption:
步骤 1.启动主备数据库，判断数据库状态；
步骤 2.修改备机数据库控制文件权限为000；
步骤 3.恢复权限
步骤 4.备机数据库重启
'''
import os
import unittest
import _thread
import time
from yat.test import macro
from yat.test import Node
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import *


class Deletaduit(unittest.TestCase):
    commonshpri = CommonSH('PrimaryDbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.rootNode = Node()
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case148.py start------------')
        self.startCmd = f'source {macro.DB_ENV_PATH};gs_om -t start'
        self.statusCmd = f'source {macro.DB_ENV_PATH};gs_om -t status --detail'
        self.com = Common()
        self.dbUserNode1 = Node(node='PrimaryDbUser')
        self.dbUserNode = Node(node='PrimaryRoot')
        self.dbUserNode2 = Node(node='Standby1Root')
        self.dbUserNode3 = Node(node='Standby1DbUser')

    def test_startdb(self):
        # 修改备机数据库控制文件权限为000；
        self.log.info('chmod 000 ' + macro.DB_INSTANCE_PATH + '/global/pg_control')
        self.dbUserNode2.sh('chmod 000 ' + macro.DB_INSTANCE_PATH + '/global/pg_control')

    def tearDown(self):
        # 恢复权限
        self.log.info('chmod 600 ' + macro.DB_INSTANCE_PATH + '/global/pg_control')
        self.dbUserNode2.sh('chmod 600 ' + macro.DB_INSTANCE_PATH + '/global/pg_control')
        # 备机数据库重启
        msg1 = self.dbUserNode3.sh(self.startCmd).result()
        self.log.info(msg1)
        self.assertIn('Successfully started', msg1)
        status = self.dbUserNode1.sh(self.statusCmd).result()
        if not status:
            raise Exception("db status is not true, please check!")
        else:
            pass
        self.log.info(
            '------------------------------------reliability_dbsys148.py finish------------------------------------')
